package com.jinwei.qunguanjia.system.service;

import com.jinwei.qunguanjia.system.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * 用户权限处理
 * 
 * @author ruoyi
 */
@Service("sysPermissionService")
public class SysPermissionServiceImpl implements SysPermissionService {

    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysMenuService sysMenuService;

    /**
     * 获取角色数据权限
     * @return 角色权限信息
     */
    public Set<String> listRolePermission()
    {
        Set<String> roles = new HashSet<>();
        // 管理员拥有所有权限
        if (SecurityUtils.isAdmin()) {
            roles.add("admin");
        } else {
            roles.addAll(sysRoleService.listRolePermissionByUserId(SecurityUtils.getUserId()));
        }
        return roles;
    }

    /**
     * 获取菜单数据权限
     * @return 菜单权限信息
     */
    public Set<String> listMenuPermission(String userId) {
        Set<String> perms = new HashSet<>();
        // 管理员拥有所有权限
        if (SecurityUtils.isAdmin(userId)) {
            perms.add("*:*:*");
        }else {
            perms.addAll(sysMenuService.listMenuPermsByUserId(userId));
        }
        return perms;
    }
}
